package org.itheima.googleplay.ui.holder;

import org.itheima.googleplay.R;
import org.itheima.googleplay.bean.AppInfoBean;
import org.itheima.googleplay.utils.LogUtils;
import org.itheima.googleplay.utils.UIUtils;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * @包名: org.itheima.googleplay.ui.holder
 * @类名: AppDetailDesHolder
 * @作者: 肖琦
 * @创建时间 : 2015-3-24 上午11:20:09
 * 
 * @描述: TODO
 * 
 * @SVN版本: $Rev: 20 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-24 16:01:24 +0800 (星期二, 24 三月 2015) $
 * 
 * @更新描述:
 */
public class AppDetailDesHolder extends BaseHolder<AppInfoBean> implements OnClickListener
{

	private static final long	DURATION	= 300;

	@ViewInject(R.id.app_detail_des_tv_des)
	private TextView			mTvDes;

	@ViewInject(R.id.app_detail_des_tv_author)
	private TextView			mTvAuthor;

	@ViewInject(R.id.app_detail_des_iv_arrow)
	private ImageView			mIvArrow;

	private boolean				isOpened	= true; // 当前是否是否打开的

	@Override
	protected View initView()
	{
		View view = View.inflate(UIUtils.getContext(), R.layout.app_detail_des, null);

		// 注入
		ViewUtils.inject(this, view);

		// 给点击事件
		view.setOnClickListener(this);

		return view;
	}

	@Override
	protected void refreshUI(AppInfoBean data)
	{
		// 设置数据
		mTvAuthor.setText(UIUtils.getString(R.string.app_detail_des_author, data.author));
		mTvDes.setText(data.des);

		// 让文本收起来
		toggle(false);
	}

	@Override
	public void onClick(View v)
	{
		toggle(true);
	}

	private void toggle(boolean animated)
	{

		if (isOpened)
		{
			// 需要关闭
			// 让textView的高度改变

			int start = getOpenedHeight();
			int end = getClosedHeight();

			if (start < end)
			{
				int tmp = start;
				start = end;
				end = tmp;
			}

			if (animated)
			{
				// start --> end
				doDesTextAnimation(start, end);
			}
			else
			{
				setDesTextHeight(end);
			}
		}
		else
		{
			int start = getClosedHeight();// 取小的值
			int end = getOpenedHeight();

			if (start > end)
			{
				int tmp = start;
				start = end;
				end = tmp;
			}

			// 需要打开
			if (animated)
			{
				// start --> end 由小到大
				doDesTextAnimation(start, end);
			}
			else
			{
				setDesTextHeight(end);
			}
		}

		// 更改状态
		isOpened = !isOpened;

		// 箭头的旋转
		if (animated)
		{
			if (isOpened)
			{
				ObjectAnimator.ofFloat(mIvArrow, "rotation", 0, 180).setDuration(DURATION).start();
			}
			else
			{
				ObjectAnimator.ofFloat(mIvArrow, "rotation", 180, 360).setDuration(DURATION).start();
			}
		}
		else
		{
			mIvArrow.setImageResource(isOpened ? R.drawable.arrow_up : R.drawable.arrow_down);
		}
	}

	/**
	 * 完全显示的高度
	 * 
	 * @return
	 */
	private int getOpenedHeight()
	{
		TextView tv = new TextView(UIUtils.getContext());
		tv.setText(mData.des);
		// tv.setLines(7);// 设置textView 的行数

		int measuredWidth = mTvDes.getMeasuredWidth();
		int measuredHeight = mTvDes.getMeasuredHeight();

		LogUtils.d("textView的宽度:" + measuredWidth);
		LogUtils.d("textView的高度:" + measuredHeight);

		// 测量
		int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
		int heightMeasureSpec = 0;
		tv.measure(widthMeasureSpec, heightMeasureSpec);

		return tv.getMeasuredHeight();
	}

	private int getClosedHeight()
	{
		// 获得行高
		// int height = mTvDes.getLineHeight();
		// return height * 7;

		TextView tv = new TextView(UIUtils.getContext());
		tv.setText(mData.des);
		tv.setLines(7);// 设置textView 的行数

		int measuredWidth = mTvDes.getMeasuredWidth();
		int measuredHeight = mTvDes.getMeasuredHeight();

		LogUtils.d("textView的宽度:" + measuredWidth);
		LogUtils.d("textView的高度:" + measuredHeight);

		// 测量
		int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
		int heightMeasureSpec = 0;
		tv.measure(widthMeasureSpec, heightMeasureSpec);

		return tv.getMeasuredHeight();
	}

	private void doDesTextAnimation(int start, int end)
	{
		ValueAnimator animator = ValueAnimator.ofInt(start, end);
		animator.setDuration(DURATION);
		animator.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animator)
			{
				int value = (Integer) animator.getAnimatedValue();

				setDesTextHeight(value);
			}
		});

		animator.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animator arg0)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator animator)
			{
				// 动画介绍时
				// 让scrollView 滑动
				ScrollView scrollView = getScrollView();
				if (scrollView != null)
				{
					// 滑动到底部

					scrollView.fullScroll(View.FOCUS_DOWN);
				}

			}

			@Override
			public void onAnimationCancel(Animator arg0)
			{
				// TODO Auto-generated method stub

			}
		});
		animator.start();
	}

	/**
	 * 设置高度
	 * 
	 * @param value
	 */
	private void setDesTextHeight(int value)
	{
		// 改变TextView的高度
		LayoutParams params = mTvDes.getLayoutParams();
		params.height = value;
		mTvDes.setLayoutParams(params);
	}

	private ScrollView getScrollView()
	{
		ViewParent parent = mRootView.getParent();

		while (parent != null)
		{
			if (parent instanceof ScrollView) { return (ScrollView) parent; }

			parent = parent.getParent();
		}

		return null;
	}
}
